package com.soft.util.decrypt;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.soft.util.encrypt.Aes;


public class DecryptResponse {

    public static String parseResponse(String result ,String appSecret) {
        if (result==null || result.length()<1){
            return null;
        }
        try {
            JSONObject resultJson = new JSONObject(result);
            if (resultJson!= null){
                String code = resultJson.getStr("code");
                if (code.equals("0")) {
                    String dataStr = resultJson.getStr("data");
                    if (dataStr != "" && dataStr != null&&!dataStr.equals("null")&&!dataStr.equals("{}")){
                        String decryptDataStr = Aes.decryptAes(dataStr, appSecret);
                        if(decryptDataStr != null&&!decryptDataStr.equals("null")&&!decryptDataStr.equals("")){
                            if (decryptDataStr.startsWith("{")){
                                JSONObject decryptDataJson = new JSONObject(decryptDataStr);
                                resultJson.put("data", decryptDataJson);
                            }
                            if (decryptDataStr.startsWith("[")) {
                                JSONArray decryptDataJson = new JSONArray(decryptDataStr);
                                resultJson.put("data", decryptDataJson);
                            }
                        }

                    }
                }
            }
            return resultJson.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

   /* public static void main(String[] args) {
        String str1 = "{\n" +
                "    \"code\":0, \"data\":\"Njc4YzNlYzgwN2M3MTI2YmE0Nzc0ZmMzNGZjMzA5ZDAxZWU5YjM1MGYxZWJiNWJhZDJhNTk3OTg5NzQzYWNmM2NjY2RmYmE3NTJlOTUxNGIwNmE1MThhNzliYWFjY2FmZTY0MmZmMTQ5YWUxYTliODY1MmI5ZGNhYTc4NTMzMzg1M2IwNGQ1ODUxMWNlM2YxMzFmNjkwOGY3OGU4NmYwOGYwODY3ZmMxNjY4NWFhOTcxN2VkZTk4NTdiNTYxYzkxMzY4YjAxMTcwZTI0ODQ1M2M5M2Y5YTdkYzlhOWQ2NzY3ZTc0MDg0NDhjNjUyNTJmZWVhMDJjNjFhMjNkYTk3ZjMwYzkwYzgxMzg5YWY1YzVkMGZhNTllZmE3M2U5NTY0YmVmOTM5M2ZmYTI1Y2JmODFlZWZhNTAzN2FiZGY4MGE5ZjNkZmVlZGMyYjBiMTRkYTVmY2QwZjQ3NGFhMmFhNA==\",\n" +
                "    \"desc\":\"ok\"\n" +
                "}==";
        String str2 = "UnCwsnSDNQmz4c0zgPE9jnLsWiCqHtqY";
        String str3 = DecryptResponse.parseResponse(str1,str2);
        System.out.println(str3);
    }*/
}
